package panda.android.notebook.File;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import panda.android.notebook.view.WordModel;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;


public class FileUtils {

	/**
	 * 创建数据库
	 * */
	private SQLiteDatabase CreateDB(String filename) {
		String dbPath = Environment.getExternalStorageDirectory() + "/note";
		File dbp = new File(dbPath);
		File dbf = new File(dbPath + "/" + filename);

		if (!dbp.exists()) {
			dbp.mkdir();
		}
		// 数据库文件是否创建成功
		if (!dbf.exists()) {
			try {
				dbf.createNewFile();
				SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbf,
						null);
				String str_sql = "CREATE TABLE  img ("
						+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
						+ "_type REAL," + "_path TEXT," + "_x REAL,"
						+ "_y REAL," + "_width REAL," + "_height REAL,"
						+ "_img BLOB);";
				db.execSQL(str_sql);
				System.out.println("建表完成");
				return db;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("文件存在 删除重建");
			dbf.delete();
			return null;
			// 文件存在
		}
		return null;
	}

	private SQLiteDatabase getDB(String filename) {
		String dbPath = Environment.getExternalStorageDirectory() + "/note";
		File dbf = new File(dbPath + "/" + filename);
		if (!dbf.exists())
			return null;
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbf, null);
		return db;
	}

	public void SaveFile(List<WordModel> bitmapMDs, String filename) {
		SQLiteDatabase db = CreateDB(filename);
		if (db == null)
			db = CreateDB(filename);
		db.beginTransaction();
		for (int i = 0; i < bitmapMDs.size(); i++) {
			WordModel bitmapMD = bitmapMDs.get(i);
			ContentValues initValues = new ContentValues();
			initValues.put("_type", bitmapMD.getType());
			initValues.put("_path", bitmapMD.getPath());
			initValues.put("_x", bitmapMD.getX());
			initValues.put("_y", bitmapMD.getY());
			initValues.put("_width", bitmapMD.getWidth());
			initValues.put("_height", bitmapMD.getHeight());
			if (bitmapMD.getBitmap() != null) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				bitmapMDs.get(i).getBitmap()
						.compress(Bitmap.CompressFormat.PNG, 100, baos);
				initValues.put("_img", baos.toByteArray());
			}
			db.insert("img", null, initValues);// 保存数据
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public List<WordModel> openFile(String filename,Activity mActivity) {
		List<WordModel> bitmapMDs = new ArrayList<WordModel>();
		SQLiteDatabase db = getDB(filename);
		if (db == null)
			return null;

		String[] col = { "_x", "_y", "_width", "_height", "_img", "_type",
				"_path" };
		Cursor cursor = db.query("img", col, null, null, null, null, null);// 数据的查询

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			WordModel bitmapMD = new WordModel(mActivity,cursor.getInt(5));
			bitmapMD.setX(cursor.getInt(0));
			bitmapMD.setY(cursor.getInt(1));
			bitmapMD.setWidth(cursor.getInt(2));
			bitmapMD.setHeight(cursor.getInt(3));
			bitmapMD.setType(cursor.getInt(5));
			bitmapMD.setPath(cursor.getString(6));
			
			byte[] data = cursor.getBlob(4);
			if (data != null)
				bitmapMD.setBitmap(BitmapFactory.decodeByteArray(data, 0,
						data.length));
			bitmapMDs.add(bitmapMD);

		}
		cursor.close();
		db.close();
		return bitmapMDs;

	}
}
